﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KAIERP
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AB_Click(object sender, EventArgs e)
        {
            AA01 aa = new AA01();
            aa.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //先获取SO_SOMmain最大值
            string somainIDsql=$@" declare @maxid int
				                 SELECT @maxid=iFatherId+1 FROM ufsystem..UA_Identity WHERE cVouchType='SOMain' and cAcc_Id=994
				                 update  ufsystem..UA_Identity set iFatherId=@maxid WHERE cVouchType='SOMain' and cAcc_Id=994 
                                 SELECT @maxid";
            object PomainID = Maticsoft.DBUtility.DbHelperSQL.GetSingle(somainIDsql);
            int SOMainID=int.Parse(PomainID.ToString());
            Maticsoft.Model.SO_SOMain model=new Maticsoft.Model.SO_SOMain();
            model.cSTCode = "01";//销售类型
            model.dDate = DateTime.Today;
            model.cSOCode = "X002";
            model.cCusCode = "0112001";//华为客户编码
            model.cDepCode = "04";//部门编码 营业部
            model.cPersonCode = "120829003";//业务编码界面上要填业务员 余晓姣
            model.cexch_name = "越南盾";
            model.iExchRate = 1;//汇率越南盾汇率
            model.iTaxRate = 13;//表头税率
            model.iStatus = 0;//未审核状态
            model.cMaker = "郑英灿";//制单人
            model.bDisFlag = false;
            model.bReturnFlag= false;
            model.cCusName = "华为";
            model.bOrder = false;//是否生成物料计划
            model.ID = SOMainID+ 1000000000;//需要从ERP生成ID
            model.iVTid = 95;//单据模板号
            //model.ufts 不用增加，　 时间戳，SQL SERVER 会自动补齐
            model.cBusType = "普通销售";
            model.dPreMoDateBT= DateTime.Today;
            model.dPreDateBT= DateTime.Today;
            model.iverifystate= 0;//审核状态
            model.iswfcontrolled = 0;//是否控制工作流
            model.ccusperson = "郭靖";//客户联系人
            model.dcreatesystime= DateTime.Now;
            model.bcashsale = false;//现款结算
            model.ccuspersoncode = "0112001001";//联系人编码 在客户联系人档案里面
            model.bmustbook= false;
            Maticsoft.BLL.SO_SOMain bll = new Maticsoft.BLL.SO_SOMain();
            bll.Add(model);//表头增加
            //子表
            Maticsoft.Model.SO_SODetails modelson=new Maticsoft.Model.SO_SODetails();
            modelson.cSOCode = model.cSOCode;
            modelson.cInvCode = "0US311GP0392";
            modelson.dPreDate = DateTime.Today;
            modelson.iQuantity = 900;
            modelson.iQuotedPrice= 0;//报价
            modelson.iTaxUnitPrice = 1.2M;
            modelson.iUnitPrice= (decimal)(modelson.iTaxUnitPrice / 1.13M);//原币无税单价           
            modelson.iMoney= modelson.iQuantity* modelson.iUnitPrice;//原税无税金额
            modelson.iSum = modelson.iQuantity * modelson.iTaxUnitPrice; //原币价税合计
            modelson.iTax = modelson.iSum - modelson.iMoney;//原币税额
            modelson.iDisCount = 0;
            modelson.iNatUnitPrice= modelson.iUnitPrice * model.iExchRate;//本币无税单价
            modelson.iNatMoney = modelson.iMoney * model.iExchRate;//本币无税金额
            modelson.iNatTax = modelson.iTax* model.iExchRate;//本币税额
            modelson.iNatSum = modelson.iSum * model.iExchRate; ;//本币价税合计
            modelson.iNatDisCount= 0;
            string soDEtailsql = $@" declare @maxid int
				                 SELECT @maxid=iChildId+1 FROM ufsystem..UA_Identity WHERE cVouchType='SOMain' and cAcc_Id=994
				                 update  ufsystem..UA_Identity set iChildId=@maxid WHERE cVouchType='SOMain' and cAcc_Id=994 
                                 SELECT @maxid";
            object sodeID = Maticsoft.DBUtility.DbHelperSQL.GetSingle(soDEtailsql);
            int SOMainsonID = int.Parse(sodeID.ToString());
            modelson.iSOsID= SOMainsonID + 1000000000;//销售订单子表标识
            modelson.KL = 100; //扣率
            modelson.KL2= 100;//扣率2
            //获取存货编码资料
            Maticsoft.BLL.Inventory ibll=new Maticsoft.BLL.Inventory();
            Maticsoft.Model.Inventory imodel = ibll.GetModel(modelson.cInvCode);
            modelson.cInvName =imodel.cInvName;//存货名称
            modelson.iTaxRate = 13;
            modelson.ID = model.ID;//销售订单主表标识
            modelson.fSaleCost= 0;//零售单价
            modelson.fSalePrice= 0;//零售金额
            modelson.dPreMoDate = DateTime.Today.AddDays(20);
            modelson.iRowNo = 1;
            modelson.fcusminprice= 0;//客户最低售价
            modelson.ballpurchase = false;//是否全部采购
            modelson.bOrderBOM= false;//是否订单BOM
            modelson.bOrderBOMOver = 0;//订单BOM是否完成
            modelson.busecusbom= false;//是否使用客户BOM          
            Maticsoft.BLL.SO_SODetails bllson=new Maticsoft.BLL.SO_SODetails();
            bllson.Add(modelson);
            MessageBox.Show("写入成功");
        }
    }
}
